import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = []

const router = new VueRouter({
  mode: 'history', // 去掉路径中的 # 号
  routes: [
    {
      path: '/',
      redirect: '/login'
    },

    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录'
      },
      component: () => import('@/views/Login.vue')
    },
    
    {
      path: '/register',
      name: 'register',
      meta: {
        title: '注册'
      },
      component: () => import('@/views/Register.vue')
    },

    {
      path: '/home',
      meta:{
        title: '主页',
      },
      component: () => import('@/views/Home.vue'),
      redirect: '/index',
      
      //子路由
      children:[
        {
          path: '/index',
          meta:{
            title: '首页'
          },
          component: () => import('@/views/Welcome.vue')
        },
        {
          path: '/user/list',
          meta: {
            title: '用户管理'
          },
          component: () => import('@/views/user/Index.vue'),
        },
        {
          path: '/user/detail',
          meta: {
            title: '用户详情'
          },
          component: () => import('@/views/user/Detail.vue'),
        },
        {
          path: '/course/list',
          meta: {
            title: '课程管理'
          },
          component: () => import('@/views/course/Index.vue'),
        },
        {
          path: '/course/add',
          meta: {
            title: '新增课程'
          },
          component: () => import('@/views/course/Add.vue'),
        },
        {
          path: '/course/update',
          meta: {
            title: '编辑课程'
          },
          component: () => import('@/views/course/Add.vue'),
        },
        {
          path: '/course/detail',
          meta: {
            title: '课程详情'
          },
          component: () => import('@/views/course/Detail.vue'),
        },
        {
          path: '/article/index',
          meta: {
            title: '文章管理'
          },
          component: () => import('@/views/article/index.vue'),
        },
        {
          path: '/order/index',
          meta: {
            title: '订单管理'
          },
          component: () => import('@/views/order/index.vue'),
        },
        {
          path: '/system/authority',
          meta: {
            title: '权限管理'
          },
          component: () => import('@/views/system/authority.vue'),
        },
        {
          path: '/article/add',
          meta: {
            title: '新增文章'
          },
          component: () => import('@/views/article/add.vue')
        },
        {
          path: '/order/add',
          meta: {
            title: '新增订单'
          },
          component: () => import('@/views/order/add.vue')
        }
      ]
    }
  ]
})


//挂载路由：to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作
router.beforeEach( (to, from, next) => {
  if(to.meta.title){
    document.title = '融创云海 - ' + to.meta.title
  }
  //放行登陆页面
  if(to.path === '/login'){
    return next()
  }
  // 获取token
  // const token= sessionStorage.getItem('token')
  // if (!token) {
  //   return next('/login')
  // } else {
  //   next()
  // }
  return next()
})

export default router
